//Edit by AiYan, 11839387

package user.ucm;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import user.model.SituModel;
import user.model.UserModel;

public class UCLogining implements UCController{

    private UserModel um;
    private SituModel sm;

    public UCLogining(UserModel um, SituModel sm) {
        this.um = um;
        this.sm = sm;
    }

    public int run() {
        try {
            Class.forName("com.mysql.jdbc.Driver");

            Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/cp3120?user=root&password=root");
            con.setCatalog("cp3120");

            Statement statement = con.createStatement();
            String search = "SELECT UserName,Password FROM userinfo where UserName='" + um.getName() + "'";

            ResultSet test = statement.executeQuery(search);
            if (!test.first()) {
                return 1;
            } else {
                test.beforeFirst();
                while (test.next()) {
                    String psw = test.getString("Password");
                    if (psw.equalsIgnoreCase(String.valueOf(um.getPsw()))) {
                        return 0;
                    } else {
                        return 2;
                    }
                }

            }

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }



        return 0;
    }
}
